package cn.demomaster.quickrouter.manager;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.util.Log;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;

import cn.demomaster.quickrouter.lifecycle.LifecycleRecorder;
import cn.demomaster.quickrouter.lifecycle.LifecycleStateType;


public class QuickFragmentLifecycleCallbacks extends FragmentManager.FragmentLifecycleCallbacks {

    public static String TAG = QuickFragmentLifecycleCallbacks.class.getName();

    @Override
    public void onFragmentCreated(@NonNull FragmentManager fm, @NonNull Fragment f, @Nullable Bundle savedInstanceState) {
        super.onFragmentCreated(fm, f, savedInstanceState);
        record(LifecycleStateType.onFragmentCreated,f);
    }

    @Override
    public void onFragmentSaveInstanceState(@NonNull FragmentManager fm, @NonNull Fragment f, @NonNull Bundle outState) {
        super.onFragmentSaveInstanceState(fm, f, outState);
        record(LifecycleStateType.onFragmentSaveInstanceState,f);
    }

    @Override
    public void onFragmentStarted(@NonNull FragmentManager fm, @NonNull Fragment f) {
        super.onFragmentStarted(fm, f);
        record(LifecycleStateType.onFragmentStarted,f);
    }

    @Override
    public void onFragmentResumed(@NonNull FragmentManager fm, @NonNull Fragment f) {
        super.onFragmentResumed(fm, f);
        record(LifecycleStateType.onFragmentResumed,f);
    }

    @Override
    public void onFragmentPaused(@NonNull FragmentManager fm, @NonNull Fragment f) {
        super.onFragmentPaused(fm, f);
        record(LifecycleStateType.onFragmentPaused,f);
    }

    @Override
    public void onFragmentStopped(@NonNull FragmentManager fm, @NonNull Fragment f) {
        super.onFragmentStopped(fm, f);
        record(LifecycleStateType.onFragmentStopped,f);
    }

    @Override
    public void onFragmentDestroyed(@NonNull FragmentManager fm, @NonNull Fragment f) {
        super.onFragmentDestroyed(fm, f);
        record(LifecycleStateType.onActivityDestroyed,f);
    }

    @Override
    public void onFragmentAttached(FragmentManager fm, Fragment fragment, Context context) {
        super.onFragmentAttached(fm, fragment, context);
        record(LifecycleStateType.onFragmentAttached,fragment);
    }

    @Override
    public void onFragmentDetached(FragmentManager fm, Fragment fragment) {
        super.onFragmentDetached(fm, fragment);
        record(LifecycleStateType.onFragmentDetached,fragment);
    }

    private void record(LifecycleStateType lifecycleStateType,Fragment fragment) {
        log(fragment,lifecycleStateType);
        LifecycleRecorder.getInstance().recordFragmentEvent(
                fragment,
                lifecycleStateType
        );
    }
    private void log(Fragment  fragment, LifecycleStateType lifecycleStateType) {
        Log.i(TAG, String.format("[%s@%d] %s",fragment.getClass().getSimpleName(),fragment.getClass().hashCode(),lifecycleStateType+""));
    }
}